import Vue from 'vue'
import VueRouter, { RouteConfig } from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)
const routeFiles = require.context('./modules', false, /\.ts$/);
/* eslint-disable-next-line */
const routeModules: RouteConfig = routeFiles.keys().reduce((modules: any, modulePath: string) => {
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1');
  const value = routeFiles(modulePath);
  modules[moduleName] = value.default;
  return modules;
}, {});

const routes: Array<RouteConfig> = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  ...Object.values(routeModules),
]

const router = new VueRouter({
  mode: process.env.VUE_APP_ROUTE_MODE || 'history',
  base: process.env.VUE_APP_ROUTE_BASE || '/',
  routes
})

export default router
